package com.huosu.utils;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.NumberUtil;
import com.amihaiemil.eoyaml.Yaml;
import com.amihaiemil.eoyaml.YamlMapping;
import com.huosu.commons.enums.NetworkModeEnum;
import com.huosu.domain.Net;

import java.io.File;
import java.io.IOException;


/**
 * @program: miner-shield-web-v2
 * @description:
 * @author: changlin
 * @create: 2019-12-12 09:57
 **/
public class NetPlanUtil {

    /**
     * 修改 netplan 配置文件
     */
    public static void writeNetPlanYaml(Net netPaln2, String path) {

        YamlMapping enp1s0 = Yaml.createYamlMappingBuilder().add("dhcp4", "yes").build();

        if (NetworkModeEnum.STATIC_IP.getCode().intValue() == netPaln2.getDhcp4().intValue()) {

            String ip = netPaln2.getIpAddress() + "/" + IPv4Util.getNetMask(netPaln2.getSubnetMask());

            enp1s0 = Yaml.createYamlMappingBuilder()
                    .add("gateway4", netPaln2.getGateway())
                    .add("addresses", Yaml.createYamlSequenceBuilder()
                            .add(ip)
                            .build())
                    .add("nameservers", Yaml.createYamlMappingBuilder()
                                    .add("addresses", Yaml.createYamlSequenceBuilder()
                                            .add(netPaln2.getDns()).build()
                                    )
//                            .add("search", Yaml.createYamlSequenceBuilder()
//                                    .add("114.114.114.114").build()
//                            )
                                    .build()
                    )
                    .build();
        }

        YamlMapping yaml = Yaml.createYamlMappingBuilder()
                .add("network", Yaml.createYamlMappingBuilder()
                        .add("ethernets", Yaml.createYamlMappingBuilder()
                                .add("enp2s0", Yaml.createYamlMappingBuilder()
                                        .add("dhcp4", "false").build())
                                .add("enp3s0", Yaml.createYamlMappingBuilder()
                                        .add("dhcp4", "false").build())
                                .add("enp1s0", enp1s0).build()
                        )
                        .add("bridges", Yaml.createYamlMappingBuilder()
                                .add("br0", Yaml.createYamlMappingBuilder()
                                        .add("interfaces",
                                                Yaml.createYamlSequenceBuilder()
                                                        .add("enp2s0")
                                                        .add("enp3s0")
                                                        .build())
                                        .build())
                                .build())
                        .add("version", "2")
                        .build()
                ).build();
        FileUtil.writeUtf8String(yaml.toString(), path);
    }

    /**
     * 读取
     */
    public static Net readNetPlanYaml(String path) {
        Net net = new  Net();
        try {
            YamlMapping yamlMapping = Yaml.createYamlInput(new File(path))
                    .readYamlMapping();
            String addresses = yamlMapping.yamlMapping("network").yamlMapping("ethernets").yamlMapping("enp1s0").yamlMapping("addresses").toString();
            addresses = addresses.substring(addresses.indexOf("-")+1);
            String ipAddresses = addresses.substring(0,addresses.indexOf("/")).trim();
            String iSubnetMask = addresses.substring(addresses.indexOf("/")+1,addresses.length()).trim();
            String subnetMask = IPv4Util.getNetMask(NumberUtil.parseInt(iSubnetMask));
            String gateway4 = yamlMapping.yamlMapping("network").yamlMapping("ethernets").yamlMapping("enp1s0").string("gateway4").toString();
            String nameservers = yamlMapping.yamlMapping("network").yamlMapping("ethernets").yamlMapping("enp1s0").yamlMapping("nameservers").yamlMapping("addresses").toString();
            nameservers = nameservers.substring(nameservers.indexOf("-")+1);
            nameservers = nameservers.trim();
            net.setDhcp4(0);
            net.setIpAddress(ipAddresses);
            net.setSubnetMask(subnetMask);
            net.setGateway(gateway4);
            net.setDns(nameservers);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return net;
    }

}
